package reflection;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class Reflection {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        Cat cat = new Cat();
        cat.hi();
        Properties properties = new Properties();
        properties.load(new FileInputStream("src/reflection/re.properties"));
        String classfullpath = properties.get("classfullpath").toString();
        String method = properties.get("method").toString();

        Class<?> cls = Class.forName(classfullpath);
        Object obj = cls.newInstance();
        Method method1 = cls.getMethod(method);
        //反射 方法invoke对象 反过来
        //反射只需要修改配置文件不需要修改源码
        method1.invoke(obj);
        //这样只能得到公有成员变量
        Field name =  cls.getField("baka");
        System.out.println(name.get(obj));

        //获取构造器
        //无参构造器
        Constructor<?> constructor = cls.getConstructor();

    }
}
